<!--pages/traffic/paySuccess.wxml-->
<template>
  <view>
    <view class="box">
      <image :src="imgUrl+'/sentry1.png'" class="box-img1" />
      <image :src="imgUrl+'/sentry2.png'" class="box-img2" />
    </view>
    <view class="btn" @tap="callPhone()">
      <image :src="imgUrl+'/sentry3.png'" class="btn-img" />
      <text>打电话</text>
    </view>
    <view v-if="!hasPhone">
      <view class="panel-black"></view>
      <view class="panel-box">
        <image :src="imgUrl+'/close.png'" class="panel-box-close" @tap="hasPhone=true"/>
        <view class="panel-box-t1">提示</view>
        <view class="panel-box-t2">尚未获取手机号，无法反馈</view>
        <button
          openType="getPhoneNumber"
          @GetPhoneNumber="onGetPhoneNumber"
          class="panel-box-btn"
          style="height:100rpx;"
        >获取手机号</button>
      </view>
    </view>
    <!-- <view class="btn" @tap="goBack">确定</view> -->
  </view>
</template>
<script>
import Taro from "@tarojs/taro";
import { setGlobalData, getGlobalData } from "../../globalData.js";
import authorize from "../../authorize.js";
import { request } from "../../util/axios.js";
import "./fault.less";
import bulterApi from "@souche2014/bulter-api";
const api = bulterApi({ partner: "IOParking" });
export default {
  data() {
    return {
      isRecharge: false,
      hasPhone: true,
      mobile: "",
      appId: "",
      coupon: null,
      apiBaseUrl: process.env.IOParking.API_HOST, //请求头
      imgUrl: process.env.IOParking.IMG_URL,
      parkId: 0,
      isReceive: false, //是否领取到优惠券
      couponParkId: "",
      userPhone: "0371-61313389",
      channelCode:''
    };
  },
  mixins: [authorize],
  onLoad: function(options) {
     console.log("参数:", options);
    let that = this;
    if (options.channelCode) {
      //体验开发版本扫码获得参数
      console.log("体验扫码参数:", options);
      that.channelCode = options.channelCode ? options.channelCode : "";
    } else if (options.q) {
      //正式版扫码获得参数
      let qrUrl = decodeURIComponent(options.q);
      that.channelCode = that.getQueryString(qrUrl, "channelCode");
      console.log("正式扫码channelCode:", that.channelCode);
    }
    if (!getGlobalData("openId")) {
      that.wx_login();
    }
  },
  methods: {
    goBack() {
      Taro.redirectTo({
        url: "../home/home"
      });
    },
    
    initData() {
      let params = {
        mobile: getGlobalData("mobile"),
        plate: "",
        channelClientId: this.channelCode
      };
      request("/orderchannel/create",params).then(res => {
        if (res.isSuccess) {
          
        }
      });
    },
    callPhone() {
      console.log(getGlobalData("mobile"));
      var that = this;
      if (getGlobalData("mobile") == "" || !getGlobalData("mobile")) {
        that.hasPhone = false;
        return;
      }
      that.initData()
      Taro.makePhoneCall({
        phoneNumber: this.userPhone //仅为示例，并非真实的电话号码
      });
      
    },
    //解析链接方法
    getQueryString(url, name) {
      var reg = new RegExp("(^|&|/?)" + name + "=([^&|/?]*)(&|/?|$)", "i");
      var r = url.substr(1).match(reg);
      if (r != null) {
        return r[2];
      }
      return null;
    },
    wx_login() {
      var that = this;
      api
        .getUserInfo({
          scope: "all"
        })
        .then(resp => {
          if (resp.success) {
            console.log("authCode is：", resp.data);
            if (resp.data == "mock") {
              setGlobalData("openId", "oRPxj5ZrsZGRuxTa-UrzEBeLuv-s");
              setGlobalData("mobile", "17761643218");
              return;
            }
            that.getInfos(resp.data);
          } else {
            console.log("err is ", resp.message, "err code is ", resp.code);
          }
        });
    },
    getInfos(authCode) {
      var that = this;
      Taro.showLoading({
          title: '加载中',
      })
      Taro.request({
        url: this.apiBaseUrl + "/weixin/souchegetuser?authCode=" + authCode,
        data: {},
        header: {
          "content-type": "application/json" // 默认值
        },
        method: "GET",
        success(res) {
          Taro.hideLoading();
          console.log("获取用户信息：", res.data);
          if (res.data.isSuccess) {
            var userOpenId = res.data.data.openId;
            setGlobalData("openId", userOpenId);
            setGlobalData("mobile", res.data.data.phoneNum);
          }
        }
      });
    },
    onGetPhoneNumber({ detail }) {
      const { encryptedData, iv } = detail;
      if (encryptedData && iv) {
        this.authPhone(encryptedData, iv);
        this.hasPhone = true;
        Taro.showLoading({
          title: "授权中，请稍后",
          mask: true
        });
      }
    },
    authPhone(encryptedData, iv) {
      api
        .register({
          encryptedData: encryptedData,
          iv: iv,
          scope: "all"
        })
        .then(resp => {
          Taro.hideLoading();
          if (resp.success) {
            console.log("用户注册 ", resp.data);
            if (resp.data == "mock") {
              setGlobalData("mobile", "17761643218");
              this.mobile = getGlobalData("mobile")
                ? getGlobalData("mobile")
                : "";
              return;
            }
            this.getInfos(resp.data);
          } else {
            console.log("err is ", resp.msg, "err code is ", resp.code);
          }
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>

